package com.leen.framework.config;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * mvc类说明
 *
 * @author admin
 * @date 2021-05-17
 */
public class MvcConfig extends WebMvcConfigurationSupport {

    /**
     * 配置消息转换  json
     * @param converters
     */
    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

        FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();
        //中文乱码的问题
        List<MediaType> types = new ArrayList<MediaType>();
        types.add(MediaType.APPLICATION_JSON_UTF8);
        fastConverter.setSupportedMediaTypes(types);
        fastConverter.setDefaultCharset(StandardCharsets.UTF_8);

        converters.add(fastConverter);

        //添加对String类型转换
        converters.add(responseBodyConverter());

        //5、追加默认转换器
        super.addDefaultHttpMessageConverters(converters);
    }


    public HttpMessageConverter<String> responseBodyConverter() {
        StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        return converter;
    }



}
